package com.snail.snailvr.network;

import android.util.Log;

import com.android.volley.Request;
import com.android.volley.Response;
import com.snail.snailvr.VRApp;
import com.snail.snailvr.model.BannerModel;
import com.snail.snailvr.model.Tag;
import com.snail.snailvr.model.UpdateModel;
import com.snail.snailvr.network.upload.FormImage;
import com.snail.snailvr.network.upload.PostUploadRequest;
import com.snail.snailvr.utils.JsonUrl;
import com.snail.snailvr.utils.NetworkUtil;
import com.snail.snailvr.utils.Util;

import java.util.List;

/**
 * 所有接口请求的一个封装
 * Created by sunxy on 2015/11/20.
 */
public class VRApi {
    /**
     * 版本更新
     */
    public static void requestVersionUpdateJson(String cVersionCode, final IFSResponse callback) {
        newGet(JsonUrl.JSON_URL_VERSION_UPDATE + "&cVersionCode=" + cVersionCode, Tag.TAG_VERSION_UPDATE, UpdateModel.class, false, false, false, callback);
    }

    /**
     * 上传图片接口
     */
    public static void uploadImg(int score, List<FormImage> imageList, Response.Listener listener, Response.ErrorListener errorListener,final IFSResponse callback, String content, String email){
        String url = JsonUrl.JSON_URL_FEEDBACK+"?cImei="+Util.getIMEI(VRApp.getInstance())+"&cPhoneType="+Util.getPhoneType()+"&cOs=1&iScore="+score+"&cContext="+content+"&cEmail="+email+"&locale="+Util.getCurrentLanguage() ;
        if(imageList!=null&&!imageList.isEmpty()) {
            Request request = new PostUploadRequest(url, imageList, listener, errorListener);
            VRApp.getRequestQueue().add(request);
        }else{
            newPost(url, Tag.TAG_VERSION_UPDATE, UpdateModel.class, false, false, false, callback);
        }
    }

    /**
     * 获取发现页面的Banner
     */
    public static void getBanner(final IFSResponse callback){
        newGet(JsonUrl.JSON_URL_LIVE_BANNER, Tag.TAG_LIVE_BANNER, BannerModel.class, false, true, false, callback);
    }

    /**
     * get请求
     */
    private static void newGet(String url, String tag, Class classZ, boolean shouldCache, boolean extendJson
            , boolean isHomeJson, final IFSResponse callback) {
        newRequests(Request.Method.GET,url, tag, classZ, shouldCache, extendJson, isHomeJson, callback);

    }
    /**
     * post请求
     */
    private static void newPost(String url, String tag, Class classZ, boolean shouldCache, boolean extendJson
            , boolean isHomeJson, final IFSResponse callback) {
        newRequests(Request.Method.POST,url, tag, classZ, shouldCache, extendJson, isHomeJson, callback);
    }

    private static void newRequests(int method,String url, String tag, Class classZ, boolean shouldCache, boolean extendJson, boolean isHomeJson, IFSResponse callback) {
        if (NetworkUtil.isNetworkConnected()) {
            if (callback != null)
                RequestHelper.newGetRequest(method,url, tag, classZ, callback, shouldCache, extendJson, isHomeJson);
            Log.i("update","url:" + url);
        }
    }


}
